package com.disruptor.demo;

import com.lmax.disruptor.RingBuffer;

/**
 * 订单事件生产者
 *
 * @创建者: 宋晓虎
 * @创建时间: 2025/11/2  19:52
 */
public class OrderEventProducer {

    private RingBuffer<OrderEvent>ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void sendData(long price){
        //获取序列
        long sequene = ringBuffer.next();
        try {
            //获取预先分配的事件，初步怀疑这个事件是调用工厂类预先设置好的
            OrderEvent orderEvent = ringBuffer.get(sequene);
            orderEvent.setPrice(price);
        }finally {
            //发布之后消息接收者才能接收消息
            ringBuffer.publish(sequene);
        }

    }
}

